package gulimall.coupon.service.impl;

import gulimall.coupon.dao.MemberPriceDao;
import gulimall.coupon.dao.SkuLadderDao;
import gulimall.coupon.entity.MemberPriceEntity;
import gulimall.coupon.entity.SkuLadderEntity;
import gulimall.coupon.feign.ProductFeignService;
import gulimall.coupon.service.MemberPriceService;
import gulimall.to.MemberPrice;
import gulimall.to.SkuReductionTo;
import gulimall.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import gulimall.utils.PageUtils;
import gulimall.utils.Query;

import gulimall.coupon.dao.SkuFullReductionDao;
import gulimall.coupon.entity.SkuFullReductionEntity;
import gulimall.coupon.service.SkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Resource
    private SkuFullReductionDao skuFullReductionDao;
    @Resource
    private SkuLadderDao skuLadderDao;
    @Resource
    private MemberPriceService memberPriceService;
    @Resource
    private ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存sku优惠相关信息：满减，折扣，会员价格
     *
     * @param skuReductionTo
     */
    @Override
    @Transactional
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        //1.保存sku的满减信息(sms_sku_full_reduction)
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTo, skuFullReductionEntity);
        skuFullReductionEntity.setAddOther(skuReductionTo.getPriceStatus());//设置是否可叠加优惠
        if (skuReductionTo.getFullCount() > 0) {
            skuFullReductionDao.insert(skuFullReductionEntity);
        }

        //2.保存sku的折扣信息(sms_sku_ladder)
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        BeanUtils.copyProperties(skuReductionTo, skuLadderEntity);
        skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());//设置是否可叠加优惠
        //获取sku基本信息==>>获取sku价格(远程服务调用)
        //TODO 这里需要用到分布式事务seata,不然读取不到product模块mysql读未提交的数据
        /*R r = productFeignService.skuPrice(skuReductionTo.getSkuId());
        BigDecimal price = (BigDecimal) r.get("price");
        BigDecimal discountPrice = price.multiply(skuReductionTo.getDiscount());
        skuLadderEntity.setPrice(discountPrice);//设置打折后价格*/
        if (skuReductionTo.getFullPrice().compareTo(new BigDecimal(0)) > 0) {
            skuLadderDao.insert(skuLadderEntity);
        }

        //3.保存sku会员价格相关信息(sms_member_price)
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        if (memberPrice != null && !memberPrice.isEmpty()) {
            List<MemberPriceEntity> memberPriceEntities = memberPrice.stream().map(m -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setSkuId(skuReductionTo.getSkuId());//设置skuId
                memberPriceEntity.setMemberLevelId(m.getId());//设置会员等级id
                memberPriceEntity.setMemberLevelName(m.getName());//设置会员等级名称
                memberPriceEntity.setMemberPrice(m.getPrice());//设置会员价格
                memberPriceEntity.setAddOther(1); //是否叠加其他优惠，默认设为叠加

                return memberPriceEntity;
            }).filter(m -> {//筛选出设置了会员价格的部分，过滤掉没有设置会员价格的部分
                int i = m.getMemberPrice().compareTo(new BigDecimal(0));
                return i > 0;
            }).collect(Collectors.toList());
            memberPriceService.saveBatch(memberPriceEntities);
        }
    }

}